const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topic: '',
    phoneNumber:'',
    maxLength: 20,
    currentLength: 0,
    type: "text",
    key: "",
    label: "",
    code: '',
    codeTime: 0,
    codeStatus: false,
    isOver:true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // wx.setNavigationBarTitle({
    //   title: "绑定新手机号码"
    // })
    this.setData({
      topic: options.value || "",
      // phoneNumber: options.value || "",
      maxLength: options.max,
      currentLength: options.value.length,
      type: options.type,
      key: options.key,
      label: options.label
    })
  },
  topicInput: function(e) {
    var topic = e.detail.value,
      currentLength = topic.length;
    if (currentLength > this.data.maxLength) {
      wx.showToast({
        title: '输入文字数量已达到上限',
        icon: 'none',
        duration: 2000
      })
      return this.data.topic;
    }
    this.setData({
      // topic: topic,
      phoneNumber:topic,
      currentLength: currentLength
    })
  },
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },
  sendCode(){
    var phone = this.data.phoneNumber.trim();
    var phonePattern = /^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|5|7|8][0-9]{9}))$/;
    if (phone.length == 0) {
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!phonePattern.test(phone)) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    // if (phone == this.data.topic) {
    //   wx.showToast({
    //     title: "新手机号码与当前手机号一致，请重新输入",
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return;
    // }
    var that = this;
    app.req({
      url:"wechat/user/checkTel",
      data:{
        telPhone: phone,
      },
      method:"post",
      success:function(res){
        if(res.code == 200){
          that.getcode(phone)
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getcode(phone){
    var that = this
    app.req({
      url: "wechat/sys/sendCode?telphone=" + phone + '&type=' + 1,
      data: {},
      method: "post",
      success: function (res) {
        console.log(res)
        if (200 == res.code) {
          wx.showToast({
            title: '验证码发送成功，请注意查收',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            codeTime: 60,
            codeStatus: true
          })
          var currentTime = that.data.codeTime
          var interval = setInterval(function () {
            currentTime--;
            that.setData({
              codeTime: currentTime
            })
            if (currentTime == 0) {
              clearInterval(interval)
              that.setData({
                codeTime: 0,
                codeStatus: false
              })
            }
          }, 1000)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  save: function() {
    var data = {};
    var that = this;
    var val = that.data.phoneNumber.trim();
    var label = that.data.label;
    if (!val) {
      wx.showToast({
        title: label + "必填",
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var type = that.data.type;
    switch (type) {
      case "phone":
        var phonePattern = /^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|5|7|8][0-9]{9}))$/;
        if (!phonePattern.test(val)) {
          wx.showToast({
            title: label + "格式错误",
            icon: 'none',
            duration: 2000
          })
          return;
        }
        if (that.data.code == '') {
          wx.showToast({
            title: "请输入验证码",
            icon: 'none',
            duration: 2000
          })
          return;
        }
        break;
      case "email":
        var emailPattern = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
        if (!emailPattern.test(val)) {
          wx.showToast({
            title: label + "格式错误",
            icon: 'none',
            duration: 2000
          })
          return;
        }
        break;
      case "realNameType":
        var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
        if (!reg.test(val)) {
          wx.showToast({
            title: label + "格式错误",
            icon: 'none',
            duration: 2000
          })
          return;
        }
        break;
    }
    data[that.data.key] = val;
    if (that.data.key == 'telphone'){
      if(that.data.isOver){
        that.setData({
          isOver:false
        })
        app.req({
          url: "wechat/user/updateTel",
          data: {
            code: this.data.code,
            tel: this.data.phoneNumber.trim()
          },
          method: "post",
          success: function (res) {
            if (res.code == 200) {
              that.setData({
                isOver:true
              })
              let pages = getCurrentPages(),
                prevPage = pages[pages.length - 2];
              prevPage.toggle('user.telphone', this.data.phoneNumber);
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
              // that.saveInfo(data);
            } else {
              that.setData({
                isOver: true
              })
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
   
    }else{
      that.saveInfo(data);
    }
  },
  saveInfo(data){
    app.TestBuriedPoint('yqlw_tabb_data_submission')
    var that = this;
    app.getUserInfo(function (userInfo) {
      app.req({
        url: "wechat/user/updateUserInformation",
        data: data,
        method: "post",
        success: function (res) {
          if (200 == res.code) {
            let pages = getCurrentPages(),
              prevPage = pages[pages.length - 2];
            prevPage.toggle('user.' + that.data.key, that.data.phoneNumber);
            wx.navigateBack({
              delta: 1
            })
          } else {
            // console.log(res.msg)
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  }
})